<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    如何友好的把Python和Bash结合在一起.md |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-other/如何友好的把Python和Bash结合在一起"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  如何友好的把Python和Bash结合在一起.md
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/11/other/%E5%A6%82%E4%BD%95%E5%8F%8B%E5%A5%BD%E7%9A%84%E6%8A%8APython%E5%92%8CBash%E7%BB%93%E5%90%88%E5%9C%A8%E4%B8%80%E8%B5%B7/" class="article-date">
  <time datetime="2020-11-10T16:00:00.000Z" itemprop="datePublished">2020-11-11</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/other/">other</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.1k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">7 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="如何友好的把Python和Bash结合在一起"><a href="#如何友好的把Python和Bash结合在一起" class="headerlink" title="如何友好的把Python和Bash结合在一起"></a>如何友好的把Python和Bash结合在一起</h1><p>对于Linux用户而言，命令行操作我们已经非常熟悉了。与其他流行的操作系统不同，在Linux社区中，使用命令行与使用图形用户界面执行类似任务相比，命令行通常可以提供更优雅，更有效的解决方案。</p>
<p><img src="http://iubest.gitee.io/pic/640-1601004011096.webp" alt="img"></p>
<p>随着Linux社区对命令行的依赖不断增长，UNIX shell（如bash和zsh）已发展成为极其强大的工具，可以补充UNIX shell的经验。使用bash和其他类似的shell，可以使用许多强大的功能，例如管道，文件名通配符以及从称为脚本的文件中读取命令的功能。</p>
<p>让我们看一个真实的示例来演示命令行的功能。每次用户登录服务时，其用户名都会记录到一个文本文件中。对于此示例，让我们找出有多少唯一用户使用该服务。</p>
<p>以下示例中的一系列命令通过将较小的构建块链接在一起，显示了更复杂的实用工具的功能：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ cat names.log | sort | uniq | wc -l</span><br></pre></td></tr></table></figure>

<p>管道符号（|）用于将一个命令的标准输出传递到下一命令的标准输入。在此处的示例中，cat names.txt的输出传递到sort命令中。sort命令的输出是按字母顺序重新排列文件的每一行。随后将其传递给uniq命令，该命令将删除所有重复的名称。最后，uniq的输出传递到wc命令。wc是计数命令，并且设置了-l标志，它返回行数。这使您可以将许多命令链接在一起。</p>
<p>但是，有时所需的内容可能会变得非常复杂，并且将命令链接在一起可能变得笨拙。在这种情况下，shell脚本就是答案。Shell脚本是由Shell读取并按顺序执行的命令列表。Shell脚本还支持某些编程语言基础知识，例如变量，流控制和数据结构。Shell脚本对于将经常重复运行的批处理作业非常有用。不幸的是，shell脚本有一些缺点：</p>
<p>Shell脚本很容易变得过于复杂，并且对于想要改进或维护它们的开发人员来说是不可读的。</p>
<p>这些shell脚本的语法和解释器通常很笨拙且不直观。语法越笨拙，对于必须使用这些脚本的开发人员来说，可读性就越差。</p>
<p>该代码通常无法在其他脚本中使用。脚本之间的代码重用往往很困难，并且脚本往往非常特定于某个问题。</p>
<p>用于高级功能（例如HTML解析或HTTP请求）的库不像现代编程和脚本语言那样容易获得。</p>
<p>这些问题会使shell脚本难以处理，并经常导致大量开发人员时间浪费。取而代之的是，Python编程语言可以用作非常有力的替代品。使用Python代替Shell脚本有很多好处：</p>
<p>默认情况下，所有主要的Linux发行版都安装了Python。打开命令行并立即键入python，将使您进入Python解释器。这种普遍性使它成为大多数脚本任务的明智选择。</p>
<p>Python具有非常易于阅读和理解的语法。它的风格强调简约和简洁的代码，同时允许开发人员以适合shell脚本的准系统风格进行编写。</p>
<p>Python是一种解释型语言，这意味着没有编译阶段。这使Python成为编写脚本的理想语言,它允许您以解释的方式快速尝试新代码。这使开发人员可以快速修改，而不必将整个程序写到文件中。</p>
<p>Python是一种功能齐全的编程语言。代码重用很简单，因为Python模块可以轻松导入并在任何Python脚本中使用。脚本可以轻松扩展或构建。</p>
<p>Python可以使用出色的标准库和数以千计的第三方库来处理各种高级实用程序，例如解析器和请求库。例如，Python的标准库包括日期时间库，该库允许将日期解析为指定的任何格式并将其与其他日期轻松比较。</p>
<p>但Python不应替换所有bash命令。编写以UNIX方式运行的Python程序（即读入标准输入并写入标准输出）与为现有的shell命令（如cat和sort）编写Python替代品一样强大。</p>
<p>让我们以本文前面已解决的问题为基础。除了已经完成的工作之外，让我们找出某个用户已登录系统的次数。uniq命令仅删除重复项，但不提供有关有多少重复项的信息。代替uniq，Python脚本可以用作链中的另一个命令。这是一个执行此操作的Python程序（在我的示例中，我将此文件称为namescount.py）：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">#!&#x2F;usr&#x2F;bin&#x2F;env python</span><br><span class="line">import sys</span><br><span class="line"></span><br><span class="line">if __name__ &#x3D;&#x3D; &quot;__main__&quot;:</span><br><span class="line">    # Initialize a names dictionary as empty to start with.</span><br><span class="line">    # Each key in this dictionary will be a name and the value</span><br><span class="line">    # will be the number of times that name appears.</span><br><span class="line">    names &#x3D; &#123;&#125;</span><br><span class="line">    # sys.stdin is a file object. All the same functions that</span><br><span class="line">    # can be applied to a file object can be applied to sys.stdin.</span><br><span class="line">    for name in sys.stdin.readlines():</span><br><span class="line">            # Each line will have a newline on the end</span><br><span class="line">            # that should be removed.</span><br><span class="line">            name &#x3D; name.strip()</span><br><span class="line">            if name in names:</span><br><span class="line">                    names[name] +&#x3D; 1</span><br><span class="line">            else:</span><br><span class="line">                    names[name] &#x3D; 1</span><br><span class="line"></span><br><span class="line">    # Iterating over the dictionary,</span><br><span class="line">    # print name followed by a space followed by the</span><br><span class="line">    # number of times it appeared.</span><br><span class="line">    for name, count in names.iteritems():</span><br><span class="line">            sys.stdout.write(&quot;%d\t%s\n&quot; % (count, name))</span><br></pre></td></tr></table></figure>

<p>让我们看看这个Python脚本如何适合命令链。首先，它从通过<code>sys.stdin</code>对象公开的标准输入中读取输入。任何输出都将写入<code>sys.stdout</code>对象，这是在Python中实现标准输出的方式。Python字典（在其他语言中通常称为哈希映射）用于获取从用户名到重复计数的映射。要获得所有用户的数量，请执行以下操作：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ cat names.log | python namescount.py</span><br></pre></td></tr></table></figure>

<p>显示用户出现的次数以及用户名的计数。接下来要做的是按顺序显示最常使用该系统的用户。这可以在Python级别完成，但是让我们使用核心UNIX实用程序已经提供的实用程序来实现它。以前，我使用<code>sort</code>命令对字母进行排序。如果命令提供了<code>-rn</code>标志，它将按降序对行进行数字排序。随着Python脚本按标准输出，只需将命令通过管道传递到<code>sort</code>并检索所需的输出：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ cat names.log | python namescount.py | sort -rn</span><br></pre></td></tr></table></figure>

<p>这是将Python用作命令链一部分的强大示例。在这种情况下使用Python的优点如下：</p>
<ul>
<li>与cat和sort等工具链接的能力。简单的实用程序（逐行读取文件并以数字方式对文件进行排序）由久经考验的UNIX命令处理。这些命令也逐行读取，这意味着这些功能可以缩放到大型文件，而且速度很快。</li>
<li>当需要在链中进行一些繁重的工作时，可以编写一个非常清晰，简洁的Python脚本，该脚本将执行其所需的工作，然后将责任转移给链中的下一个链接。</li>
<li>它是一个可重用的模块，尽管此示例专门针对名称，但是如果您向此输入中包含重复行的任何输入，它将打印出每行和重复的数量。通过将Python代码模块化，可以将其应用于各种场景。</li>
</ul>
<p>为了展示以模块化和管道方式组合Python脚本的强大功能，让我们进一步把问题放大。让我们找到该服务的前五名用户。<code>head</code>是一个命令，它允许您指定一定数量的行以显示给定的标准输入。将其添加到命令链中将得到以下内容：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ cat names.log | python namescount.py | sort -rn | head -n 5</span><br></pre></td></tr></table></figure>

<p>这仅显示前五个用户，而忽略其余用户。同样，要使五个用户最少使用该服务，可以使用<code>tail</code>命令，该命令采用相同的参数。将Python命令打印到标准输出的结果使可以构建和扩展其功能。</p>
<p>以上就是简单的介绍，实际中大家灵活运用就好。</p>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/11/other/%E5%A6%82%E4%BD%95%E5%8F%8B%E5%A5%BD%E7%9A%84%E6%8A%8APython%E5%92%8CBash%E7%BB%93%E5%90%88%E5%9C%A8%E4%B8%80%E8%B5%B7/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/other/" rel="tag">other</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/11/other/%E5%A4%A7%E5%85%AC%E5%8F%B8%E4%B8%BA%E4%BB%80%E4%B9%88%E9%83%BD%E6%9C%89API%E7%BD%91%E5%85%B3%EF%BC%9F%E8%81%8A%E8%81%8AAPI%E7%BD%91%E5%85%B3%E7%9A%84%E4%BD%9C%E7%94%A8/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            大公司为什么都有API网关？聊聊API网关的作用.md
          
        </div>
      </a>
    
    
      <a href="/2020/11/11/other/%E8%A3%B8%E9%87%91%E5%B1%9E%E7%A9%B6%E7%AB%9F%E6%98%AF%E4%B8%AA%E5%95%A5/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">裸金属究竟是个啥.md</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> TzWind
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://www.baidu.com">百度</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>